package com.jsmile.cloud.authc.help.controller;

import java.util.List;

import javax.validation.Valid;

import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.jsmile.cloud.authc.api.req.sys.ReqSysHelpQuery;
import com.jsmile.cloud.authc.api.req.sys.ReqSysHelpUpdate;
import com.jsmile.cloud.authc.api.res.ResSysHelp;
import com.jsmile.cloud.authc.help.SysHelpApplicationService;
import com.jsmile.mail.security.controller.AuthcController;
import com.jsmile.mail.security.handler.JsmileContext;
import com.jsmile.mall.api.JSmileResult;
import com.jsmile.mall.api.annotation.CurrentContext;
import com.jsmile.mall.api.constants.JSmileConstant;
import com.jsmile.mall.api.enums.JSmileCacheEnum;
import com.jsmile.mall.api.enums.LogTypeEnum;
import com.jsmile.mall.api.to.JsmilePage;
import com.jsmile.mall.log.annotation.JsmileLog;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;

/**
 * @author 龚亮
 * @version 1.0
 * @desc 控制器
 * @date 2018年08月25日 04:57:24
 */
@RestController
@Api(tags = "微上手")
@Slf4j
@AllArgsConstructor
@RequestMapping("/sys/help")
public class SysHelpController extends AuthcController {

    private final SysHelpApplicationService sysHelpApplicationService;

    @RequestMapping(value = "/findAll", method = RequestMethod.POST)
    @ApiOperation(value = "帮助文档列表")
    public JSmileResult<JsmilePage<ResSysHelp>> getPageList(@RequestBody @Valid ReqSysHelpQuery reqSysHelpQuery, @CurrentContext JsmileContext jsmileContext) {
        reqSysHelpQuery.setCurTenantId(jsmileContext.getTenantId());
        IPage<ResSysHelp> pages = sysHelpApplicationService.getPageList(reqSysHelpQuery);
        return JSmileResult.success(new JsmilePage(reqSysHelpQuery.getPageIndex(), reqSysHelpQuery.getPageSize(), pages.getTotal(), pages.getRecords()));
    }

    /**
     * 查询所有分页 返回 Lit<Map>
     */
    @ApiOperation(value = "根据文档编码查询")
    @RequestMapping(value = "/getList", method = {RequestMethod.POST})
    @Cacheable(value = JSmileCacheEnum.JSmileCacheName.oneDay, key = "#help.hCode")
    public JSmileResult getList(@RequestBody @Valid ReqSysHelpQuery help, @CurrentContext JsmileContext jsmileContext) {
        List<ResSysHelp> list = sysHelpApplicationService.list(help);
        return JSmileResult.success(list);
    }

    @RequestMapping(value = "/save", method = RequestMethod.POST)
    @ApiOperation(value = "新增文档")
    @JsmileLog(title = "用户中心->新增文档", type = LogTypeEnum.INSERT, systemId = JSmileConstant.SystemId.AUTHC)
    public JSmileResult saveDict(@RequestBody @Valid ReqSysHelpUpdate reqSysHelpUpdate, @CurrentContext JsmileContext jsmileContext) {
        this.initContext(jsmileContext, JSmileConstant.SystemId.AUTHC);
        reqSysHelpUpdate.setTenantId(jsmileContext.getTenantId());
        reqSysHelpUpdate.setCreateBy(jsmileContext.getUserName());
        reqSysHelpUpdate.setUpdateBy(jsmileContext.getUserName());
        if (!sysHelpApplicationService.saveHelp(reqSysHelpUpdate)) {
            return JSmileResult.error("添加文档异常!");
        }
        return JSmileResult.success();
    }

    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @ApiOperation(value = "修改文档")
    @JsmileLog(title = "用户中心->修改文档", type = LogTypeEnum.UPDATE, systemId = JSmileConstant.SystemId.AUTHC)
    public JSmileResult updateDict(@RequestBody @Valid ReqSysHelpUpdate reqSysHelpUpdate, @CurrentContext JsmileContext jsmileContext) {
        this.initContext(jsmileContext, JSmileConstant.SystemId.AUTHC);
        reqSysHelpUpdate.setTenantId(jsmileContext.getTenantId());
        reqSysHelpUpdate.setCreateBy(jsmileContext.getUserName());
        reqSysHelpUpdate.setUpdateBy(jsmileContext.getUserName());
        if (!sysHelpApplicationService.updateHelp(reqSysHelpUpdate)) {
            return JSmileResult.error("修改文档异常!");
        }
        return JSmileResult.success();
    }

    @RequestMapping(value = "/enable", method = RequestMethod.POST)
    @ApiOperation(value = "起停用文档")
    @JsmileLog(title = "用户中心->起停用文档", type = LogTypeEnum.UPDATE, systemId = JSmileConstant.SystemId.AUTHC)
    public JSmileResult enableHelp(@RequestBody @Valid ReqSysHelpUpdate reqSysHelpUpdate, @CurrentContext JsmileContext jsmileContext) {
        this.initContext(jsmileContext, JSmileConstant.SystemId.AUTHC);
        reqSysHelpUpdate.setTenantId(jsmileContext.getTenantId());
        reqSysHelpUpdate.setCreateBy(jsmileContext.getUserName());
        reqSysHelpUpdate.setUpdateBy(jsmileContext.getUserName());
        if (!sysHelpApplicationService.enableHelp(reqSysHelpUpdate)) {
            return JSmileResult.error("修改文档异常!");
        }
        return JSmileResult.success();
    }

    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    @ApiOperation(value = "删除文档")
    @JsmileLog(title = "用户中心->删除文档", type = LogTypeEnum.DELETE, systemId = JSmileConstant.SystemId.AUTHC)
    public JSmileResult deleteDict(@RequestBody @Valid ReqSysHelpUpdate reqSysHelpUpdate, @CurrentContext JsmileContext jsmileContext) {
        this.initContext(jsmileContext, JSmileConstant.SystemId.AUTHC);
        reqSysHelpUpdate.setTenantId(jsmileContext.getTenantId());
        if (!sysHelpApplicationService.deleteHelp(reqSysHelpUpdate)) {
            return JSmileResult.error("删除文档异常!");
        }
        return JSmileResult.success();
    }
}
